import { Component, OnInit, AfterViewChecked } from '@angular/core';
import { UserVO } from '../vo/UserVO';
import { ApiUrl } from '../api/ApiUrl';
import { Router } from '@angular/router';
import { ToastController, Events } from '@ionic/angular';
import { ApiService } from '../api/api.service';
import { ArticleVO } from '../vo/ArticleVO';
@Component({
  selector: 'app-mine',
  templateUrl: './mine.page.html',
  styleUrls: ['./mine.page.scss'],
})
export class MinePage implements OnInit, AfterViewChecked {
  userItem: UserVO;
  curl: string;
  constructor(private toastController: ToastController, public api: ApiService, private router: Router,
    private events: Events) { }

  ngOnInit() {
    this.curl = 'assets/image/avatar.png';
    if (ApiUrl.user != null) {
      this.userItem = ApiUrl.user;
      // 初始化用户头像
      if (null != this.userItem.cportrait && this.userItem.cportrait.length > 0) {
        this.curl = ApiUrl.headUrl + this.userItem.cportrait + '?time=' + new Date().getTime();
      }
    } else {
      this.userItem = null;
    }
  }
  ngAfterViewChecked() {
    this.curl = 'assets/image/head.png';
    if (ApiUrl.user != null) {
      this.userItem = ApiUrl.user;
      // 初始化用户头像
      if (null != this.userItem.cportrait && this.userItem.cportrait.length > 0) {
        this.curl = ApiUrl.headUrl + this.userItem.cportrait + '?time=' + new Date().getTime();
      }
    } else {
      this.userItem = null;
    }
  }
  // 检测更新
  checkForUpdate() {
    this.presentToast('已是最新版本');
  }

  // 帮助手册、关于我们
  toArticle(type) {
    this.toDetail(type);
  }
  toDetail(type) {
    this.api.getItem<ArticleVO>(ApiUrl.getFirstArticleChecked,
      { 'articletypeiid': type }).subscribe(response => {
        if (response === null) {
          console.log('没有数据返回');
          return;
        }
        const articleItem: ArticleVO = response;
        console.log(response);
        const ctitle: string = (type === -1) ? '关于我们' : '帮助手册';
        if (null == articleItem || null == articleItem.curl || articleItem.curl.length === 0) {
          this.presentToast('未找到' + ctitle + '内容');
          return;
        }
        const curl: string = ApiUrl.twUrl + articleItem.curl;
        this.router.navigate(['/showview', { 'curl': curl, 'ctitle': ctitle }]);
      }, () => {

        console.log('出错了');
      });
  }

  login() {
    this.router.navigate(['/login']);
  }
  logout() {
    ApiUrl.user = null;
    ApiUrl.txflag = false;
    // 发布事件
    this.events.publish('userlogin:change', false, Date.now());
    this.router.navigate(['/tabs/(tab1:tab1)']);
  }
  myconnectcompany() {
    if ( this.userItem != null && this.userItem.iconsultantid !== 0) {
      this.router.navigate(['/myconnectcompany']);
    } else {
      this.presentToast('只有金融顾问有权限访问！');
    }
  }
  myconnectbank() {
    console.log(this.userItem.iusertype);
    if ( this.userItem != null && this.userItem.iusertype + '' === '3') {
      this.router.navigate(['/myconnectbank']);
    } else {
      this.presentToast('只有企业用户有权限访问！');
    }
  }
  visit() {
    if ( this.userItem != null) {
      console.log(this.userItem.iusertype);
      if (this.userItem.iusertype + '' === '1' || this.userItem.iusertype + '' === '2') {
        this.router.navigate(['/visit']);
      } else {
        this.presentToast('只有金融顾问有权限访问！');
      }
    } else {
      this.presentToast('只有金融顾问有权限访问！');
    }
  }
  connect() {
    if ( this.userItem == null ) {
      this.router.navigate(['/connectcount']);
    } else {
      if (this.userItem.iusertype + '' === '1' || this.userItem.iusertype + '' === '2') {
        this.router.navigate(['/connect']);
      } else {
        this.router.navigate(['/connectcount']);
      }
    }
  }
  async presentToast(msg) {
    const toast = await this.toastController.create({
      message: msg,
      duration: 2000
    });
    toast.present();
  }
}
